package Sekertariat;

import PengendaliSuratDokumen.KoneksiOracle;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTable;

import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author Indrayanto
 */
public class TabelSekretariat extends javax.swing.JFrame {

    JFrame frame;
    JLabel background, logo, user, universitas, pa, pilih, nKolom;
    JButton back, ok, pil, tombolKonfirmasi;
    JRadioButton rsurat, rdok;
    JTextField konfirmasi;
    JTable table;
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    KoneksiOracle koneksiOracle = new KoneksiOracle();

    public TabelSekretariat() {
        frame = new JFrame();
        frame.setSize(1000, 600);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setResizable(true);
        frame.setTitle("Panel User");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        background = new JLabel(new ImageIcon("Background.jpg"));
        background.setBounds(0, 0, 1000, 655);
        logo = new JLabel(new ImageIcon("sadar.png"));
        logo.setBounds(10, 10, 156, 156);
        //MEMBUAT JLABEL DAN MENSETTING FONT STYLE
        user = new JLabel("Tabel Administrasi Surat Dokumen ");
        user.setFont(new Font("Serif", Font.BOLD, 26));
        user.setBounds(150, 20, 800, 40);
        user.setForeground(Color.BLACK);
        universitas = new JLabel("Universitas Sanata Dharma");
        universitas.setFont(new Font("Serif", Font.BOLD, 30));
        universitas.setForeground(Color.BLACK);
        universitas.setBounds(150, 50, 400, 40);
        pa = new JLabel("Tabel Sekretariat");
        pa.setFont(new Font("Serif", Font.BOLD, 30));
        pa.setForeground(Color.BLACK);
        pa.setBounds(150, 80, 400, 40);
        pilih = new JLabel("Pilih Tabel :");
        pilih.setFont(new Font("Arial", Font.BOLD, 14));
        pilih.setBounds(170, 150, 100, 22);

        nKolom = new JLabel("No Surat/Dok                 Perihal                     Asal                   Penerima               "
                + "Ruangan                Lemari                    Loker                    Petugas                Tanggal              Status");
        nKolom.setBounds(10, 180, 1000, 22);

        table = new JTable();
        table.setBounds(10, 200, 960, 200);

        rsurat = new JRadioButton("Surat");
        rsurat.setBounds(270, 150, 60, 22);
        rdok = new JRadioButton("Dokumen");
        rdok.setBounds(340, 150, 90, 22);
        ButtonGroup b = new ButtonGroup();
        b.add(rdok);
        b.add(rsurat);
        pil = new JButton("Pilih");
        pil.setBounds(460, 150, 70, 22);





        pil.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                lihatTabel();
            }
        });


        back = new JButton("Back");
        back.setBounds(680, 510, 100, 35);

        ok = new JButton("Ok");
        ok.setBounds(800, 510, 100, 35);

        back.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                frame.setVisible(false);

            }
        });
    }

    public void lihatTabel() {
        koneksiOracle.getDBConnection();
        conn = koneksiOracle.conn;
        if (rsurat.isSelected()) {
            try {
                String sql = "SELECT * FROM surat";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                table.setModel(DbUtils.resultSetToTableModel(rs));
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        } else if (rdok.isSelected()) {
            try {
                String sql = "SELECT * FROM dokumen";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                table.setModel(DbUtils.resultSetToTableModel(rs));

            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
            }

        }

    }

    @Override
    public void show() {
        frame.getContentPane().add(user);
        frame.getContentPane().add(universitas);
        frame.getContentPane().add(back);
        frame.getContentPane().add(ok);
        frame.getContentPane().add(logo);
        frame.getContentPane().add(pa);
        frame.getContentPane().add(pilih);
        frame.getContentPane().add(rsurat);
        frame.getContentPane().add(rdok);
        frame.getContentPane().add(pil);
        frame.getContentPane().add(table);
        frame.getContentPane().add(nKolom);
        frame.getContentPane().add(background);

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                TabelSekretariat tbs = new TabelSekretariat();
                tbs.show();
            }
        });
    }
}
